﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Maniac
{
    public partial  class ProgressWindow : Form
    {
        public ProgressWindow()
        {
            InitializeComponent();
            this.Load += new EventHandler(ProgressWindow_Load);

            this.Shown += new EventHandler(ProgressWindow_Shown);
        }

        void ProgressWindow_Shown(object sender, EventArgs e)
        {
            //timer1.Enabled = true;            
			initiateWatchlistDownload(MainWatchlist);
        }
        public WatchList MainWatchlist;
        private int iviSecurityEnum = 0;
        
        void ProgressWindow_Load(object sender, EventArgs e)
        {
            progressBar1.Minimum = 0;
            progressBar1.Maximum = MainWatchlist.Securities.Count;
            progressBar1.Value = 0;

            label1.Text = "Processing: " + MainWatchlist.Securities[iviSecurityEnum].Symbol;

            StartPosition = FormStartPosition.CenterParent;
        }
		public void DoSpecificDownload(Security tvsItem)
		{
				Console.WriteLine(DateTime.Now.Date.ToString());
				Console.WriteLine(tvsItem.LastDownloadTime.Date.ToString());
			    tvsItem.PriceDownloader = new YahooDataDownloader();
                                                
                 tvsItem.PriceDownloader.Security = tvsItem;
                 tvsItem.PriceDownloader.DownloadPricesCompleted += new EventHandler(tvYahoo_DownloadPricesCompleted);
                 tvsItem.PriceDownloader.DownloadPrices(tvsItem.LastDownloadTime);                    

		}
        public void initiateWatchlistDownload(WatchList fvwWatchlist)
        {
            foreach (Security tvsItem in fvwWatchlist.Securities)
            {

                //progressBar1.Increment(1);
                //label1.Text = "Processing: " + tvsItem.Symbol;
                //this.progressBar2.Value = 0;
				Console.WriteLine("Attempting to load " + tvsItem.Symbol + " from DB.");
				DateTime dtNow = DateTime.Now;
                //attempt to load data from disk
                if (tvsItem.HistoricalPrices.Count == 0)
                {
					Console.WriteLine("No history, try db.");
					tvsItem.persistLoad2();
					if (tvsItem.HistoricalPrices.Count == 0)
					{
						Console.WriteLine("No history, try internet.");
						DoSpecificDownload(tvsItem);
					}
					else
					{
						/*if (DateTime.Now.Date>tvsItem.LastDownloadTime.Date)
						{
							DoSpecificDownload(tvsItem);
						}
						else*/
						Console.WriteLine("History found. Count is: " + tvsItem.HistoricalPrices.Count);
						SecurityInit(tvsItem);
					}
                }
				Console.WriteLine("Completed load: " + (DateTime.Now-dtNow));
            }
            this.Close();
        }
        private void SecurityInit(Security fvsSecurity)
        {
            //this.progressBar2.Maximum = fvsSecurity.HistoricalPrices.Count;
            //this.progressBar2.Value = 0;

            fvsSecurity.ProcessingHistoricalPrice += new EventHandler(Security_ProcessingHistoricalPrice);
            
            //fvsSecurity.Calculate(iv);
            //fvsSecurity.CalculateAccuracy();
        }
        void Security_ProcessingHistoricalPrice(object sender, EventArgs e)
        {
            SecurityPriceEventArgs ee = (SecurityPriceEventArgs)e;
            //ivpProgress.label2.Text = "Processing: " + ee.Date.ToShortDateString();
            this.progressBar2.Increment(1);
			Application.DoEvents();

        }
        void tvYahoo_DownloadPricesCompleted(object sender, EventArgs e)
        {
            YahooDataDownloader tvDL = (YahooDataDownloader)sender;
            tvDL.Security.LastDownloadTime = DateTime.Now;
            SecurityInit(tvDL.Security);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {			
            //timer1.Enabled = false;
            //initiateWatchlistDownload(MainWatchlist);
            if (iviSecurityEnum > (MainWatchlist.Securities.Count-1))
            {
                timer1.Enabled = false;
                this.Close();
                return;
            }
            Security tvsItem = MainWatchlist.Securities[iviSecurityEnum];

            progressBar1.Increment(1);
            
            this.progressBar2.Value = 0;

            //attempt to load data from disk
            if (tvsItem.HistoricalPrices.Count == 0)
            {
                tvsItem.persistLoad();

				tvsItem.DateDownloader = new YahooDateDownloader();
					tvsItem.DateDownloader.Security = tvsItem;
					tvsItem.DateDownloader.DownloadDates();
				
                //if ((DateTime.Now.Date) >= tvsItem.LastDownloadTime.Date)
				//if (tvsItem.LastDownloadTime.Year == 1950)
                {
					
					
                    tvsItem.PriceDownloader = new YahooDataDownloader();

                    tvsItem.PriceDownloader.Security = tvsItem;
                    tvsItem.PriceDownloader.DownloadPricesCompleted += new EventHandler(tvYahoo_DownloadPricesCompleted);
                    //a new problem occurs with non-xml serialization.  full dl is now required.
                    tvsItem.PriceDownloader.DownloadPrices(new DateTime(1950, 1, 1));
                }
                /*else
                {
                    SecurityInit(tvsItem);
                }*/
            }
            iviSecurityEnum++;
            if (iviSecurityEnum > (MainWatchlist.Securities.Count - 1))
            {
                timer1.Enabled = false;
                this.Close();
                return;
            }
            label1.Text = "Processing: " + MainWatchlist.Securities[iviSecurityEnum].Symbol;
			Application.DoEvents();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            this.Close();
        }

        
    }
}
